<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">台车id</label>
        <el-input v-model="query.containerId" clearable placeholder="台车id" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :plain="true" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="台车id">
            <el-input v-model="form.containerId" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="台车类型">
            <el-input v-model="form.containerType" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="储位线体">
            <el-input v-model="form.storageLine" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="储位站点">
            <el-input v-model="form.storagePoint" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="储位类别">
            <el-input v-model="form.storageType" style="width: 250px;" />
          </el-form-item>
          <!-- <el-form-item label="物料规则集合">
            <el-input v-model="form.materialCodes" style="width: 250px;" />
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!-- 物料规则表单 -->
      <el-dialog title="编辑物料规则" :close-on-click-modal="false" :visible.sync="materialDialog" width="500px">
        <el-form ref="editForm" :model="editForm" size="small" label-width="120px">
          <el-form-item label="模式">
            <el-select v-model="selectMode" placeholder="请选择" style="width: 250px;" @change="handlerFirstSelectChange">
              <el-option
                v-for="item in modeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="台车id">
            <el-input v-model="editForm.containerId" readonly style="width: 250px;" placeholder="台车Id" />
          </el-form-item>
          <el-form-item v-show="selectMode === 0" label="物料">
            <el-select v-model="selectAddMaterialList" allow-create multiple filterable style="width: 250px;" placeholder="请选择" />
          </el-form-item>
          <el-form-item v-show="selectMode === 1" label="物料">
            <el-select v-model="selectMaterialList" multiple filterable style="width: 250px;" placeholder="请选择">
              <el-option
                v-for="item in materialList"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button v-if="selectMode === 1" :loading="btn.del" icon="el-icon-delete" type="danger" @click="confirmDel">确认删除</el-button>
          <el-button v-else type="primary" :loading="btn.add" icon="el-icon-plus" @click="confirmAdd">确认新增</el-button>
          <el-button @click="handlerCancel">取 消</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="containerId" label="台车id" />
        <el-table-column prop="containerType" label="台车类型" />
        <el-table-column prop="storageLine" label="储位线体" />
        <el-table-column prop="storagePoint" label="储位站点" />
        <el-table-column prop="storageType" label="储位类别" />
        <el-table-column prop="materialCodes" label="物料规则集合">
          <template slot-scope="scope">
            <!-- <div v-for="(item, index) in JSON.parse(scope.row.materialCodes.split(','))" :key="index">
              {{ item.trim() }}
            </div> -->
            <el-tooltip v-if="scope.row.materialCodes" class="item" effect="light" :content="JSON.parse(scope.row.materialCodes).join('、')" placement="top-start">
              <div>
                <template v-if="JSON.parse(scope.row.materialCodes).length >= 3">
                  {{ JSON.parse(scope.row.materialCodes)[0] }}...
                  {{ JSON.parse(scope.row.materialCodes).slice(-1)[0] }}
                </template>
                <template v-else>
                  {{ JSON.parse(scope.row.materialCodes).join(', ') }}
                </template>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','wcsContainerRule:edit','wcsContainerRule:del'])" label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <div style="display: flex;">
              <el-button type="text" @click="editMaterialRule(scope.row)">编辑物料规则</el-button>
              <udOperation
                :data="scope.row"
                :permission="permission"
              />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudWcsContainerRule from '@/api/work/wcsContainerRule'
import { delRuleAPI, addRuleAPI } from '@/api/work/wcsContainerRule'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, containerId: null, containerType: null, storageLine: null, storagePoint: null, storageType: null, materialCodes: null, createTime: null, createBy: null, updateTime: null }
export default {
  name: 'WcsContainerRule',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '台车规则管理接口', url: 'api/wcsContainerRule', idField: 'id', sort: 'id,desc', crudMethod: { ...crudWcsContainerRule },
      optShow: {
        add: true,
        reset: true,
        download: true,
        del: true,
        edit: false
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'wcsContainerRule:add'],
        edit: ['admin', 'wcsContainerRule:edit'],
        del: ['admin', 'wcsContainerRule:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'containerId', display_name: '台车id' }
      ],
      materialDialog: false,
      modeList: [
        {
          label: '新增物料规则',
          value: 0
        },
        {
          label: '删除物料规则',
          value: 1
        }
      ],
      selectMode: 0,
      materialList: [],
      selectMaterialList: [],
      selectAddMaterialList: [],
      editForm: {},
      btn: {
        add: false,
        del: false
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    editMaterialRule(row) {
      // console.log(JSON.parse(row.materialCodes))
      this.editForm.containerId = row.containerId
      // this.editform.containerId = row.containerId
      if (row.materialCodes) {
        this.materialList = JSON.parse(row.materialCodes)
      } else {
        this.materialList = []
      }
      // this.materialList = JSON.parse(row.materialCodes)
      this.materialDialog = true
    },
    handlerFirstSelectChange() {
      this.selectMaterialList = []
      this.selectAddMaterialList = []
    },
    handlerCancel() {
      this.materialDialog = false
      this.selectMaterialList = []
      this.materialList = []
      this.selectMode = 0
      this.editForm = {}
    },
    async confirmAdd() {
      if (this.selectAddMaterialList.filter(item => item).length === 0) {
        return this.$message.warning('请完善需要新增的物料规则')
      }
      // console.log(this.selectAddMaterialList)
      try {
        this.btn.add = true
        const submitForm = {
          containerId: this.editForm.containerId,
          containerCodes: this.selectAddMaterialList.filter(item => item)
        }
        const res = await addRuleAPI(submitForm)
        if (res.code === 200) {
          this.crud.toQuery()
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.message)
        }
      } finally {
        this.btn.add = false
        this.materialDialog = false
        this.selectMaterialList = []
        this.materialList = []
        this.selectMode = 0
        this.editForm = {}
        this.selectAddMaterialList = []
      }
    },
    async confirmDel() {
      if (this.selectMaterialList.filter(item => item).length === 0) {
        return this.$message.warning('请完善需要删除的物料规则')
      }
      try {
        this.btn.del = true
        const submitForm = {
          containerId: this.editForm.containerId,
          containerCodes: this.selectMaterialList.filter(item => item)
        }
        const res = await delRuleAPI(submitForm)
        if (res.code === 200) {
          this.crud.toQuery()
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.message)
        }
      } finally {
        this.btn.del = false
        this.materialDialog = false
        this.selectMaterialList = []
        this.materialList = []
        this.selectMode = 0
        this.editForm = {}
        this.selectAddMaterialList = []
      }
    }
  }
}
</script>

<style scoped>

</style>
